﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Exam.Core.Model;

public partial class Manage_editquestion : ManagePageBase
{
    protected int cid = 0;
    protected int id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Text = Resources.labels.btnok;
        Button2.Text = Resources.labels.back;

        if (string.IsNullOrEmpty(Request.QueryString["cid"])) {
            Response.End();
        }
        if (!int.TryParse(Request.QueryString["cid"], out cid)) {
            Response.End();
        }

        if (Request.QueryString["id"] == null) {
            Response.End();
        }
        if (!int.TryParse(Request.QueryString["id"], out id)) {
            Response.End();
        }
        else {
            if (!IsPostBack) {
                BindCourse();
                BindType();
                BindDiff();
                BindQuestion();
            }
        }
    }

    protected string divisshowopt = "none";
    private void BindQuestion() {
        Exam.Core.Model.Question model = new Exam.Core.DAL.Question().getModelById(id);
        QType.SelectedValue = model.QuestiontypeId.Trim();
        QDifficulty.SelectedValue = model.DifficultyId.Trim();
        QKnowledge.SelectedValue = model.QuestionknowledgeId.Trim();
        Qcontent.Text = Server.HtmlDecode(model.QuestionContent);
        QAnswer.Text = model.QuestionAnswer.Trim();
        if (model.QuestiontypeId.Trim() == "1" || model.QuestiontypeId.Trim() == "2" || model.QuestiontypeId.Trim() == "3") {
            divisshowopt = "block";
        }
        else {
            divisshowopt = "none";
        }

        List<Exam.Core.Model.QuestionOpts> opts = new Exam.Core.DAL.QuestionOpts().getOptsListByQuestionId(id);
        StringBuilder sb = new StringBuilder();
        foreach (Exam.Core.Model.QuestionOpts o in opts) {
            sb.Append(HttpUtility.HtmlDecode(o.QuestionOptsText) + "\r\n");
        }
        QOptions.Text = sb.ToString();

    }
    private void BindType() {
        List<Exam.Core.Model.QuestionType> list = new Exam.Core.DAL.QuestionType().getQuestionTypeList();
        foreach (Exam.Core.Model.QuestionType obj in list) {
            QType.Items.Add(new ListItem(obj.QuestionTypeTitle.Trim(), obj.QuestionTypeId.Trim()));
        }
        QType.Items.Insert(0, new ListItem(Resources.labels.PleaseChkQType, "0"));
        QType.Attributes.Add("onchange", "showXuanxiang(this)");
    }

    private void BindDiff() {
        List<Exam.Core.Model.QuestionDifficulty> list = new Exam.Core.DAL.QuestionDifficulty().getQuestionDifficultyList();
        foreach (Exam.Core.Model.QuestionDifficulty obj in list) {
            QDifficulty.Items.Add(new ListItem(obj.QuestionDifficultyTitle.Trim(), obj.QuestionDifficultyId.Trim()));
        }
        QDifficulty.Items.Insert(0, new ListItem(Resources.labels.PleaseChkDiff, "0"));

    }


    private void BindCourse() {
        Exam.Core.Model.Course obj = new Exam.Core.DAL.Course().getCourseById(cid);
        cname.Text = obj.Title.Trim();
        StringBuilder sb = new StringBuilder();
        foreach (Knowledge know in obj.Knowledges) {
            sb.Append(know.Title.Trim());
            sb.Append("&nbsp;&nbsp;");
            QKnowledge.Items.Add(new ListItem(know.Title.Trim(), know.Id.Trim()));
        }
        QKnowledge.Items.Insert(0, new ListItem(Resources.labels.PleaseChkKnowledge, "0"));
        cknow.Text = sb.ToString();


    }
    protected void Button1_Click(object sender, EventArgs e) {
        string QType = this.QType.SelectedValue;
        string QDifficulty = this.QDifficulty.SelectedValue;
        string QKnowledge = this.QKnowledge.SelectedValue;
        string Qcontent = Server.HtmlEncode(this.Qcontent.Text.Trim());
        string QOptions = this.QOptions.Text.Trim();
        string QAnswer = this.QAnswer.Text.Trim();

        bool flag = false;

        bool result = new Exam.Core.DAL.Question().Edit(id,Qcontent, QAnswer, QType, QDifficulty, QKnowledge, cid, "0");
        if (result) {
            if (new Exam.Core.DAL.QuestionOpts().DelByQid(id)) {
                if (QType == "1" || QType == "2" || QType == "3") {
                    string[] opts = System.Text.RegularExpressions.Regex.Split(QOptions, "\r\n");      //QOptions.Split();
                    int res = new Exam.Core.DAL.Question().AddOpts(id, opts);
                    flag = true;
                }
            }
        }
        if (flag) {
            MessageBox.AlertThenRefresh(this.Page, Resources.labels.Edit_OK);
        }
        else {
            MessageBox.AlertThenRefresh(this.Page, Resources.labels.Edit_Fail);
        }

    }
    protected void Button2_Click(object sender, EventArgs e) {
        Response.Redirect("Course.aspx?id="+cid, true);
    }
}